package com.koron.css2.contractmanage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.exception.ContractException;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.contractmanage.querybean.ContractManageQueryBean;
import com.koron.css2.contractmanage.vo.ContractHistoryVO;
import com.koron.css2.contractmanage.vo.ContractManageVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.*;

/**
 * 报装单所有合同
 */
public class ContractHistoryList implements ServerInterface {

    /**
     * 合同状态  用于数据字典取值
     */
    private static final String CONTRACT_STATUS = "CONTRACT_STATUS";

    /**
     * 合同类型  用于数据字典取值
     */
    private static final String CONTRACT_TYPE = "CONTRACT_TYPE";

    /**
     * 证件类型  用于数据字典取值
     */
    private static final String CERT_TYPE = "IDT";

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            // 校验参数
            ContractManageQueryBean queryBean = JsonUtils.objectToPojoIgnoreNone(req.getData(), ContractManageQueryBean.class);
            checkContractManageQuery(queryBean.getProjectId(), queryBean.getUserNo());

            Map<String, String> contractTypeMap = Tools.mapDicByCode(factory, CONTRACT_TYPE);
            ContractManageMapper mapper = factory.getMapper(ContractManageMapper.class);

            // 查询用户历史合同
            List<ContractManageVO> contractManageVOList = getContractHistoryList(factory, queryBean, mapper, contractTypeMap);

            ContractHistoryVO contractHistoryVO = new ContractHistoryVO();
            contractHistoryVO.setPageInfo(new PageInfo<>(contractManageVOList));

            MessageBean<ContractHistoryVO> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询报装合同成功", ContractHistoryVO.class);
            messageBean.setData(contractHistoryVO);
            return messageBean;
        } catch (Exception e) {
            logger.error("查询报装合同失败: " + e.getMessage(), e);
            e.printStackTrace();
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询报装合同失败: " + e.getMessage(), null);
        }
    }

    /**
     * 校验合同字段
     *
     * @param projectId
     * @return
     */
    public void checkContractManageQuery(String projectId, String userNo) {
        if (StringUtils.isBlank(projectId)) {
            throw new ContractException("参数中的报装单ID不能为空！");
        }

        if (StringUtils.isBlank(userNo)) {
            throw new ContractException("参数中的户号不能为空！");
        }
    }

    /**
     * 查询报装单合同
     * @return
     */
    public List<ContractManageVO> getContractHistoryList(SessionFactory factory, ContractManageQueryBean queryBean, ContractManageMapper mapper, Map<String, String> contractTypeMap) {
        Map<String, String> contractStatusMap = Tools.mapDicByCode(factory, CONTRACT_STATUS);

        // 查询报装单合同列表
        PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
        Set<String> userNos = new HashSet<>(Arrays.asList(queryBean.getUserNo().split(",")));
        List<ContractManageVO> contractManageVOList = mapper.getContractHistoryList(userNos);

        if (CollectionUtils.isNotEmpty(contractManageVOList)) {
            contractManageVOList.forEach(item -> {
                if (contractStatusMap != null && contractStatusMap.containsKey(item.getContractStatus())) {
                    item.setContractStatusName(contractStatusMap.get(item.getContractStatus()));
                }
                if (contractTypeMap != null && contractTypeMap.containsKey(item.getContractType())) {
                    item.setContractTypeName(contractTypeMap.get(item.getContractType()));
                }
                if (StringUtils.isNotBlank(item.getContractFileAddr())) {
                    if (ContractTypeEnum.CONTRACT_TYPE_3.getContractType().equals(item.getContractType())) {
                        // 普通合同拼接路径
                        item.setContractFileAddr(ApplicationConfig.getAccessoryUploadUrl() + Constant.FILE_DOWNLOAD + item.getContractFileAddr());
                    } else if (ContractTypeEnum.CONTRACT_TYPE_2.getContractType().equals(item.getContractType())) {
                        // 电子合同拼接路径
                        item.setContractFileAddr(ApplicationConfig.eleContractUrl + item.getContractFileAddr());
                    }
                }
            });
        }
        return contractManageVOList;
    }
}
